package com.huifaapp.mall.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.AlreadyPayActivity;
import com.huifaapp.mall.adapter.PendingDeliveryAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.PendingDeliveryBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 待发货
 * A simple {@link Fragment} subclass.
 */
public class PendingDeliveryFragment extends Fragment {


    private RecyclerView GoodsSend;
    private List<PendingDeliveryBean.OrderListBean>list;
    private PendingDeliveryAdapter pendingDeliveryAdapter;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pending_delivery,container,false);
        initView(view);
        GoodsSend.setLayoutManager(new LinearLayoutManager(getActivity()));
        return view;
    }

    private void initView(View view) {
        GoodsSend = (RecyclerView) view.findViewById(R.id.goodssend_rv);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notsended")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Log.i("HBHBHBHBHB",response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray orderList = jsonObject.getJSONArray("orderList");
                    Log.e("FH",orderList+"");
                    initData(response);
                } catch (JSONException e) {
                    Log.e("FH","HHH");
                }

            }
        });


    }
    private boolean isGetData = false;
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        //   进入当前Fragment
        if (enter && !isGetData) {
            isGetData = true;
            OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notsended")
                    .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                    .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    Log.i("HBHBHBHBHB",response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray orderList = jsonObject.getJSONArray("orderList");
                        Log.e("FH",orderList+"");
                        initData(response);
                    } catch (JSONException e) {
                        Log.e("FH","HHH");
                    }

                }
            });

            //   这里可以做网络请求或者需要的数据刷新操作
        } else {
            isGetData = false;
        }
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isGetData) {
            OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notsended")
                    .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                    .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    Log.i("HBHBHBHBHB",response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray orderList = jsonObject.getJSONArray("orderList");
                        Log.e("FH",orderList+"");
                        initData(response);

                    } catch (JSONException e) {
                        Log.e("FH","HHH");
                    }

                }
            });

            //   这里可以做网络请求或者需要的数据刷新操作
            isGetData = true;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isGetData = false;
    }

    private void initData(String response) {
        Gson gson = new Gson();
        final PendingDeliveryBean pendingDeliveryBean = gson.fromJson(response, PendingDeliveryBean.class);
        list = pendingDeliveryBean.getOrderList();
        pendingDeliveryAdapter = new PendingDeliveryAdapter(getActivity(), list);
        GoodsSend.setAdapter(pendingDeliveryAdapter);

        pendingDeliveryAdapter.setOnItemClickListener(new PendingDeliveryAdapter.OnItemClickListener() {
            @Override
            public void onSmall(int position) {
                Intent intent = new Intent(getActivity(), AlreadyPayActivity.class);
                intent.putExtra("orderNo",list.get(position).getOrderNo()+"");
                Log.e("jijijijijijji",list.get(position).getOrderNo()+"");
                startActivity(intent);
            }
        });
    }

}